<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="报修人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairName">
              <a-input placeholder="请输入报修人" v-model="model.createBy"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报修部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairName">
              <a-input placeholder="请输入报修部门" v-model="model.sysOrgCode_dictText"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="报修名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairName">
              <a-input v-model="model.faultRepairName" placeholder="请输入报修名称"></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="报修单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairNo">-->
<!--              <a-input v-model="model.faultRepairNo" placeholder="请输入报修单号"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <!-- <a-col :span="24">
            <a-form-model-item label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairDeviceId">
              <j-popup
                v-model="model.faultRepairDeviceId"
                field="faultRepairDeviceId"
                org-fields="device_name,device_id"
                dest-fields="deviceName,faultRepairDeviceId"
                code="ene_device"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="故障等级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairLevelId">
              <j-search-select-tag v-model="model.faultRepairLevelId" dict="ene_fault_level,fault_level_name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairTypeId">
              <j-search-select-tag v-model="model.faultRepairTypeId" dict="ene_fault_type,fault_type_name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairTime">
              <j-date
                placeholder="请选择故障时间"
                v-model="model.faultRepairTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否停机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairIsStop">
              <j-switch v-model="model.faultRepairIsStop"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairImages">
              <j-image-upload isMultiple v-model="model.faultRepairImages"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairRemarks">
              <a-input v-model="model.faultRepairRemarks" placeholder="请输入故障描述"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="故障报修状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="faultRepairState">
              <j-search-select-tag v-model="model.faultRepairState" dict="ene_fault_repair_state,fault_repair_state_name,id"  />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'EneFaultRepairForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        faultRepairName: [{ required: true, message: '请输入报修名称!' }],
        // faultRepairNo: [{ required: true, message: '请输入报修单号!' }],
        faultRepairDeviceId: [{ required: true, message: '请选择设备!' }],
        faultRepairLevelId: [{ required: true, message: '请选择故障等级!' }],
        faultRepairTypeId: [{ required: true, message: '请选择故障类型!' }],
        faultRepairTime: [{ required: true, message: '请选择故障时间!' }]
      },
      url: {
        add: '/repair/eneFaultRepair/add',
        edit: '/repair/eneFaultRepair/edit',
        queryById: '/repair/eneFaultRepair/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
.ant-form-item {
  margin-bottom: 5px !important;
}
</style>
